﻿<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:Common.Data.Converters="clr-namespace:Imagin.Common.Data.Converters;assembly=Imagin.Common"
    xmlns:Common.Extensions="clr-namespace:Imagin.Common.Extensions;assembly=Imagin.Common"
    xmlns:Controls.Common="http://imagin.tech/imagin/controls/common">
    <Common.Data.Converters:ObjectToTypeConverter x:Key="ObjectToTypeConverter"/>

    <Storyboard x:Key="FadeInAnimation">
        <DoubleAnimation Storyboard.TargetProperty="Opacity" From="0" To="1.0" Duration="0:0:0.5" AccelerationRatio="0.4" DecelerationRatio="0.4"/>
    </Storyboard>

    <Style x:Key="{x:Type ListBoxItem}" TargetType="{x:Type ListBoxItem}">
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="Foreground" Value="{DynamicResource Brush.Text}" />
        <Setter Property="Opacity" Value="0" />
        <Setter Property="Padding" Value="5" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border
                        Name="PART_Border"
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}" 
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Padding="{TemplateBinding Padding}"  
                        SnapsToDevicePixels="True">
                        <ContentPresenter />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <EventTrigger RoutedEvent="ListBoxItem.Loaded">
                <BeginStoryboard Storyboard="{StaticResource FadeInAnimation}"/>
            </EventTrigger>
            <Trigger Property="Controls.Common:ControlExtensions.IsDraggingOver" Value="True">
                <Setter Property="BorderBrush" Value="{DynamicResource Brush.DropTarget.Border}" />
            </Trigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsSelected" Value="True"/>
                    <Condition Property="Selector.IsSelectionActive" Value="True"/>
                </MultiTrigger.Conditions>
                <Setter Property="Background" Value="{DynamicResource Brush.ListBoxItem.Selected.Active.Background}"/>
                <Setter Property="Foreground" Value="{DynamicResource Brush.ListBoxItem.Selected.Active.Foreground}"/>
            </MultiTrigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsSelected" Value="True"/>
                    <Condition Property="Selector.IsSelectionActive" Value="False"/>
                </MultiTrigger.Conditions>
                <Setter Property="Background" Value="{DynamicResource Brush.ListBoxItem.Selected.Background}"/>
                <Setter Property="Foreground" Value="{DynamicResource Brush.ListBoxItem.Selected.Foreground}"/>
            </MultiTrigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsSelected" Value="False"/>
                    <Condition Property="IsMouseOver" Value="True"/>
                </MultiTrigger.Conditions>
                <Setter Property="Background" Value="{DynamicResource Brush.ListBoxItem.MouseOver.Background}"/>
                <Setter Property="Foreground" Value="{DynamicResource Brush.ListBoxItem.MouseOver.Foreground}"/>
            </MultiTrigger>
        </Style.Triggers>
    </Style>

    <Style x:Key="{x:Type ListViewItem}" TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}"/>

    <Style x:Key="Style.ListView.ScrollViewer" TargetType="{x:Type ScrollViewer}">
        <Setter Property="CanContentScroll" Value="True"/>
        <Setter Property="VerticalScrollBarVisibility" Value="Auto"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ScrollViewer">
                    <Border 
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="*"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            
                            <Border 
                                Grid.ColumnSpan="2" 
                                Background="{DynamicResource Brush.GridView.Background}"/>
                            <GridViewHeaderRowPresenter 
                                Grid.ColumnSpan="2" 
                                HorizontalAlignment="Stretch"
                                Columns="{Binding TemplatedParent.View.Columns, RelativeSource={RelativeSource TemplatedParent}}"
                                ColumnHeaderContainerStyle="{Binding TemplatedParent.View.ColumnHeaderContainerStyle, RelativeSource={RelativeSource TemplatedParent}}"
                                ColumnHeaderTemplate="{Binding TemplatedParent.View.ColumnHeaderTemplate, RelativeSource={RelativeSource TemplatedParent}}"
                                ColumnHeaderTemplateSelector="{Binding TemplatedParent.View.ColumnHeaderTemplateSelector, RelativeSource={RelativeSource TemplatedParent}}"
                                AllowsColumnReorder="{Binding TemplatedParent.View.AllowsColumnReorder, RelativeSource={RelativeSource TemplatedParent}}"
                                ColumnHeaderContextMenu="{Binding TemplatedParent.View.ColumnHeaderContextMenu, RelativeSource={RelativeSource TemplatedParent}}"
                                ColumnHeaderToolTip="{Binding TemplatedParent.View.ColumnHeaderToolTip, RelativeSource={RelativeSource TemplatedParent}}"
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>

                            <ScrollContentPresenter 
                                Grid.Row="1"
                                Name="PART_ScrollContentPresenter" 
                                CanContentScroll="{TemplateBinding CanContentScroll}"
                                KeyboardNavigation.DirectionalNavigation="Local"/>

                            <ScrollBar 
                                Name="PART_HorizontalScrollBar"
                                Orientation="Horizontal"
                                Grid.Row="2"
                                Maximum="{TemplateBinding ScrollableWidth}"
                                ViewportSize="{TemplateBinding ViewportWidth}"
                                Value="{TemplateBinding HorizontalOffset}"
                                Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"/>
                            <ScrollBar 
                                Name="PART_VerticalScrollBar"
                                Grid.Row="1"
                                Grid.Column="1"
                                Maximum="{TemplateBinding ScrollableHeight}"
                                ViewportSize="{TemplateBinding ViewportHeight}"
                                Value="{TemplateBinding VerticalOffset}"
                                Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"/>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="{x:Type ListBox}" TargetType="{x:Type ListBox}">
        <Setter Property="Background" Value="{DynamicResource Brush.ListBox.Background}"/>
        <Setter Property="BorderBrush" Value="{DynamicResource Brush.ListBox.Border}"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
        <Setter Property="Controls.Common:ListBoxExtensions.IsDirectionalSelectionEnabled" Value="True"/>
        <Setter Property="VirtualizingPanel.IsVirtualizing" Value="True"/>
        <Setter Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="True"/>
        <Setter Property="Padding" Value="5"/>
        <Setter Property="VirtualizingPanel.ScrollUnit" Value="Pixel"/>
        <Setter Property="Controls.Common:ItemsControlExtensions.SelectNoneOnEmptySpaceClick" Value="True"/>
        <Setter Property="Controls.Common:ListBoxExtensions.SelectionWrap" Value="True"/>
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Disabled"/>
        <Setter Property="VirtualizingPanel.VirtualizationMode" Value="Recycling"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBox}">
                    <ScrollViewer 
                        x:Name="PART_ScrollViewer" 
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                        <Grid>
                            <ItemsPresenter Margin="{TemplateBinding Padding}"/>
                        </Grid>
                    </ScrollViewer>
                    <ControlTemplate.Triggers>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding Mode=OneWay, Converter={StaticResource ObjectToTypeConverter}, RelativeSource={RelativeSource Self}}" Value="{x:Type ListView}"/>
                                <Condition Binding="{Binding View, Mode=OneWay, Converter={StaticResource ObjectToTypeConverter}, RelativeSource={RelativeSource Self}}" Value="{x:Type GridView}"/>
                            </MultiDataTrigger.Conditions>
                            <Setter TargetName="PART_ScrollViewer" Property="Style" Value="{StaticResource Style.ListView.ScrollViewer}"/>
                        </MultiDataTrigger>
                        <Trigger Property="IsGrouping" Value="True">
                            <Setter Property="ScrollViewer.CanContentScroll" Value="False"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="{x:Type ListView}" TargetType="{x:Type ListView}" BasedOn="{StaticResource {x:Type ListBox}}"/>
</ResourceDictionary>